define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index',
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    //del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'order_id', title: __('订单编号'), operate: 'LIKE'},
                        {field: 'title', title: __('产品名称'), operate: 'LIKE',operate: false},
                        {field: 'pay_fee', title: __('订单金额'), operate: 'LIKE',operate: false},
                        {field: 'username', title: __('购买人昵称'), operate: 'LIKE',operate: false},
                        {field: 'mobile', title: __('购买人手机号'), operate: 'LIKE',operate: false},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {'1': '待支付', '2': '已付款','-1': '已取消'}},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        /* {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate} */
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index_state: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index_state',
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('发起人(用户ID/昵称)'), operate: 'LIKE',formatter:function (value, row, index) {
                            return '【'+row.user_id+'】'+row.nickname;
                        }},
                        {field: 'title', title: __('诉求'), operate: 'LIKE'},
                        {field: 'imgs', title: __('图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'order_id', title: __('订单编号'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {'0': '待处理', '1': '已处理'}},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index_qx: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index_qx',
                    multi_url: 'order/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'nickname', title: __('发起人(用户ID/昵称)'), operate: 'LIKE',formatter:function (value, row, index) {
                            return '【'+row.user_id+'】'+row.nickname;
                        }},
                        {field: 'text', title: __('取消原因'), operate: 'LIKE'},
                        {field: 'order_id', title: __('订单编号'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), formatter: Table.api.formatter.status, searchList: {'0': '待处理', '1': '已同意','-1':'已拒绝'}},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            width: 200,
                            buttons: [
                                {
                                    name: 'status',
                                    title: '同意',
                                    text: '同意',
                                    confirm: '是否确认同意？',
                                    classname: 'btn btn-danger btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'order/setQxStatus?status=1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.status==0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                },
                                {
                                    name: 'status',
                                    title: '拒绝',
                                    text: '拒绝',
                                    confirm: '是否确认拒绝？',
                                    classname: 'btn btn-primary btn-xs btn-magic btn-ajax',
                                    url: function (row) {
                                        return 'order/setQxStatus?status=-1&id='+row.id;
                                    },
                                    visible: function (row) {
                                        if(row.status==0){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                    success: function (res){
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ],
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});